<?php

namespace Service\AuthService;

use App\Model\PssModel\Manager;
use Service\Code\Normal;
use App\Model\PssLogModel\ManagerLog;
use Service\BaseService;

class ManageWsUserAuthService extends BaseService 
{
    protected $user_id;
    protected $user_name;
    /**
     * 检测登录情况
     * @param string $token
     */
    public function checkUserLoginByToken( string $token )
    {
        $redis = redis();
        $key   = Manager::instance()->loginToken.$token;
        $user_id = $redis->get($key);
        if( ! $user_id ) {
            return Normal::returnCode(Normal::FAIL,"登录失效");
        }
        
        $manage = Manager::instance()->info(['id'=>$user_id]);
        if( empty($manage) ) {
            return Normal::returnCode(Normal::FAIL,"系统账号不存在");
        }
        
        $this->user_id = $user_id;
        $this->user_name = $manage['name'];
        return Normal::returnCode(Normal::SUC,"校验成功",$manage);
    }
    
    /**
     * 添加日志
     * @param array $param
     * @return mixed
     */
    public function addSystemLog( $param = [] )
    {
        if (is_array($param['content'])) {//数组的话转成字符串
            $param['content'] = var_export($param['content'], true);
        }
        
        $data = [
            'manager_id'    => $param['sys_user_id'],
            'manager_name'  => $param['sys_user_name'],
            'content'       => $param['content'],
            'ip'            => $param['ip']??'0.0.0.0',
            'url'           => '',
            'cd'            => time(),
        ];
        ManagerLog::instance()->add($data);
    }
}